package com.sgb.kjwl.view.ui.activity.home;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityChooseAreaBinding;
import com.sgb.kjwl.di.home.DaggerHomeComponent;
import com.sgb.kjwl.di.home.HomeModule;
import com.sgb.kjwl.model.entity.ProvinceEntity;
import com.sgb.kjwl.view.event.ChooseAreaEvent;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.ui.adapter.home.ChooseAreaAdapter;
import com.sgb.kjwl.viewmodel.home.ChooseAreaModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.mmkvutil.CacheMarker;
import com.swgk.core.mmkvutil.DatalUtils;
import com.swgk.core.mmkvutil.MkvUtils;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import javax.inject.Inject;

public class ChooseAreaActivity extends AppActivity {
    private static final String TYPE = "type";
    private ActivityChooseAreaBinding binding;
    private ChooseAreaAdapter areaAdapter;
    private List<ProvinceEntity<List<ProvinceEntity>>> provinceEntityList;
    private int index = 0;
    @Inject
    ChooseAreaModel viewModel;

    public static void start(Context context, int index) {
        Intent intent = new Intent();
        intent.putExtra(TYPE, index);
        intent.setClass(context, ChooseAreaActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerHomeComponent.builder()
                .appComponent(appComponent)
                .homeModule(new HomeModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(ChooseAreaActivity.this, R.layout.activity_choose_area);
        binding.include.actionBarImageLeft.setVisibility(View.VISIBLE);
        binding.include.actionBarImageLeft.setOnClickListener(this);
        binding.include.actionBarTitle.setText("选择省份");
        binding.tvSearch.setOnClickListener(this);

       /* binding.editSearch.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    binding.cancelView.setVisibility(View.VISIBLE);
                } else {
                    binding.cancelView.setVisibility(View.GONE);
                    binding.editSearch.setFocusable(true);
                    binding.editSearch.setFocusableInTouchMode(true);
                    closeKeybord(binding.editSearch);
                }
            }
        });*/

        if (areaAdapter == null) {
            areaAdapter = new ChooseAreaAdapter(ChooseAreaActivity.this, this);
        }
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(ChooseAreaActivity.this));
        binding.recyclerView.setAdapter(areaAdapter);

      /*  binding.editSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                viewModel.searchFilterModel(provinceEntityList, s.toString());
            }
        });*/
    }

    @Override
    protected void init() {
        index = getIntent().getIntExtra(TYPE, 0);
        areaAdapter.setCurrentArea(viewModel.getAreaNameModel());
        areaAdapter.setCommonlyUsedArea(viewModel.getCommonlyUsedArea());
        viewModel.getAllArealModel();
    }

    public void setDatas(List<ProvinceEntity<List<ProvinceEntity>>> datas) {
        this.provinceEntityList = datas;
        showAllArea(datas);
    }

    public void showAllArea(List<ProvinceEntity<List<ProvinceEntity>>> datas) {
        int select = viewModel.getSelectModel(viewModel.getAreaCodeModel(), datas);
        areaAdapter.setAllArea(datas, select);
    }

    public void chooseComlate(ProvinceEntity<List<ProvinceEntity>> listProvinceEntity) {
        viewModel.setCommonlyUsedArea(listProvinceEntity);
        EventBus.getDefault().postSticky(ChooseAreaEvent.make(EventConstant.EVENT_CHOOSE_AERA_Code, listProvinceEntity));
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.action_bar_image_left:
                finish();
                break;
            case R.id.tv_search:
                /*binding.editSearch.setText("");
                binding.editSearch.setFocusable(false);*/
                if (TextUtils.isEmpty(binding.editSearch.getText().toString())){
                    MToast.showToast(ChooseAreaActivity.this, "请输入省份名称");
                    return;
                }
                viewModel.searchFilterModel(provinceEntityList, binding.editSearch.getText().toString());
                break;
        }
    }
}
